﻿using FileZillaConfig.Helpers;
using FileZillaConfig.Types;
using System;
using System.Xml.Linq;

namespace FileZillaConfig.ConfigClasses
{
    public class Group
    {
        private string _name;
        private bool _enabled;
        private string _comments;
        private int _maxConnectionCount;
        private int _limitPerIP;
        private bool _bypassUserLimit;
        private bool _forceSSL;
        private IPFilter _ipFilter;
        private SpeedLimits _speedLimits;
        private bool _bypassServerDownloadLimit;
        private bool _bypassServerUploadLimit;
        private Folders _folders;

        #region PROPERTIES
        /// <summary>
        /// Group folders
        /// </summary>
        public Folders Folders
        {
            get { return _folders; }
        }


        /// <summary>
        /// Bypass server upload speed limit
        /// </summary>
        public bool BypassServerUploadLimit
        {
            get { return _bypassServerUploadLimit; }
            set { _bypassServerUploadLimit = value; }
        }


        /// <summary>
        /// Bypass server download speed limit
        /// </summary>
        public bool BypassServerDownloadLimit
        {
            get { return _bypassServerDownloadLimit; }
            set { _bypassServerDownloadLimit = value; }
        }


        /// <summary>
        /// Group speed limits
        /// </summary>
        public SpeedLimits SpeedLimits
        {
            get { return _speedLimits; }
        }


        /// <summary>
        /// Filter connection IPs
        /// </summary>
        public IPFilter IPFilter
        {
            get { return _ipFilter; }
        }


        /// <summary>
        /// Force SSL for user login
        /// </summary>
        public bool ForceSSL
        {
            get { return _forceSSL; }
            set { _forceSSL = value; }
        }


        /// <summary>
        /// Bypass user limit of server 
        /// </summary>
        public bool BypassUserLimit
        {
            get { return _bypassUserLimit; }
            set { _bypassUserLimit = value; }
        }


        /// <summary>
        /// Connection limit per IP
        /// </summary>
        public int ConnectionLimitPerIP
        {
            get { return _limitPerIP; }
            set { _limitPerIP = value; }
        }


        /// <summary>
        /// Maximum connection count
        /// </summary>
        public int MaximumConnection
        {
            get { return _maxConnectionCount; }
            set { _maxConnectionCount = value; }
        }


        /// <summary>
        /// Group comments
        /// </summary>
        public string Comments
        {
            get { return _comments; }
            set { _comments = value; }
        }


        /// <summary>
        /// Enable access for users inside this group
        /// </summary>
        public bool Enabled
        {
            get { return _enabled; }
            set { _enabled = value; }
        }


        /// <summary>
        /// Group name
        /// </summary>
        public string Name
        {
            get { return _name; }
            set { _name = value; }
        } 
        #endregion

        public Group(string name)
        {
            this._ipFilter = new IPFilter();
            this._speedLimits = new SpeedLimits();
            this._folders = new Folders();
            this._name = name;
        }

        public XElement getXML()
        {
            XElement Group = new XElement("Group");
            Group.Add(new XAttribute("Name", this._name));


            // User options
            Group.Add(XMLItem.Option("Bypass server userlimit", this._bypassUserLimit));
            Group.Add(XMLItem.Option("User Limit", this._maxConnectionCount));
            Group.Add(XMLItem.Option("IP Limit", this._limitPerIP));
            Group.Add(XMLItem.Option("Enabled", this._enabled));
            Group.Add(XMLItem.Option("Comments", this._comments));
            Group.Add(XMLItem.Option("ForceSsl", this._forceSSL));

            // User IPFilter
            XElement IpFilter = new XElement("IpFilter");
            IpFilter.Add(new XElement("Disallowed", this._ipFilter.Disallowed));
            IpFilter.Add(new XElement("Allowed", this._ipFilter.Allowed));

            Group.Add(IpFilter);

            // User folders
            Group.Add(this._folders.getXML());


            // User speed limits
            XElement SpeedLimits = this._speedLimits.getXML();
            SpeedLimits.Add(new XAttribute("DlType", Convert.ToInt16(this._speedLimits.DownloadType).ToString()));
            SpeedLimits.Add(new XAttribute("DlLimit", this._speedLimits.DownloadLimit.ToString()));
            SpeedLimits.Add(new XAttribute("ServerDlLimitBypass", Convert.ToInt16(this._bypassServerDownloadLimit).ToString()));
            SpeedLimits.Add(new XAttribute("UlType", Convert.ToInt16(this._speedLimits.UploadType).ToString()));
            SpeedLimits.Add(new XAttribute("UlLimit", this._speedLimits.UploadLimit.ToString()));
            SpeedLimits.Add(new XAttribute("ServerUlLimitBypass", Convert.ToInt16(this._bypassServerUploadLimit).ToString()));

            Group.Add(SpeedLimits);



            return Group;
        }
    }
}
